import './styles/main.scss'

import { createApp } from 'vue'
import App from './App.vue'
import router from './router';
import i18n from './i18n';
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import directives from './directives'
import Image from '@/components/Image.vue'
import DateFormatter from '@/components/common/DateFormatter.vue'
// 引入换算价格
import { calculatePrice } from './utils/currencyUtils'
// 引入新系统的getUserToken
import { getUserToken } from './utils/cache'
// 引入插件设置
import { setupPlugins } from './plugins'
import { useCurrencyStore } from './store/currency'
import { Local } from './utils/storage'
import '@/assets/css/global.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

// 创建Pinia实例
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)

// 创建Vue应用
const app = createApp(App);

// 将Pinia安装到应用中
app.use(pinia);

// 现在可以安全地创建store实例
const currencyStore = useCurrencyStore();

// 声明全局变量类型
declare global {
  interface Window {
    __INITIAL_PRODUCT__?: any;
  }
}
// 注册全局静态页面跳转方式
app.config.globalProperties.$push = function(url: string, query: any = {}): void {
  const type = query.type || 'product';
  if(query.type){
    delete query.type
  }
  
  router.push({
    path: `/${type}/${url}.html`,
    query: query
  });
};

// 计算价格
app.config.globalProperties.$ConvertPrice = function(price: number = 0) {
  // 添加错误处理，确保exchange_rate存在
  const exchangeRate = currencyStore.currentCurrency?.exchange_rate ?? 1;
  return calculatePrice(price, exchangeRate);
}

app.use(router);
app.use(i18n);
app.use(ElementPlus);
directives(app);

// 设置插件（包括按需引入的 Ant Design Vue 组件）
setupPlugins(app);

// 注册全局组件
app.component('Image', Image);
app.component('DateFormatter', DateFormatter);

app.mount('#app');
